package com.infodms.dms.actions.claim.basicData;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.infodms.dms.actions.common.FileUploadManager;
import com.infodms.dms.actions.sysbusinesparams.businesparamsmanage.BusinessAreaManage;
import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.Utility;
import com.infodms.dms.dao.claim.basicData.HomePageNewsDao;
import com.infodms.dms.dao.claim.dealerClaimMng.ClaimBillMaintainDAO;
import com.infodms.dms.dao.common.BaseDao;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.po.FsFileuploadPO;
import com.infodms.dms.po.TmDealerPO;
import com.infodms.dms.po.TtAsWrKnowledgebasePO;
import com.infodms.dms.po.TtAsWrModelGroupPO;
import com.infodms.dms.po.TtAsWrNewsOrgPO;
import com.infodms.dms.po.TtAsWrNewsPO;
import com.infodms.dms.util.CommonUtils;
import com.infodms.dms.util.sequenceUitl.SequenceManager;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.po3.bean.PO;
import com.infoservice.po3.bean.PageResult;

public class HomePageNews extends BaseDao {
	public Logger logger = Logger.getLogger(BusinessAreaManage.class);
	private ActionContext act = ActionContext.getContext();
	RequestWrapper request = act.getRequest();
	private final HomePageNewsDao dao = HomePageNewsDao.getInstance();
	private final ClaimBillMaintainDAO cbmDao = ClaimBillMaintainDAO.getInstance();
	private final String  addNews = "/jsp/claim/basicData/homePageNews.jsp";
	private final String  addApplicationKnowledgeBase = "/jsp/claim/basicData/addApplicationKnowledgeBase.jsp";
	private final String  modifyNews = "/jsp/claim/basicData/homePageNewsModify.jsp";
	private final String  knowledgeBaseModify = "/jsp/claim/basicData/knowledgeBaseModify.jsp";
	private final String  mainNews = "/jsp/claim/basicData/homePageNewsMain.jsp";
	private final String  mainNewsDealer = "/jsp/claim/basicData/homePageNewsMainDealer.jsp";
	private final String  knowledgeBaseDealer = "/jsp/claim/basicData/knowledgeBaseDealer.jsp";
	private final String  ApplicationKnowledgeBase = "/jsp/claim/basicData/ApplicationKnowledgeBase.jsp";
	private final String  viewNews = "/jsp/claim/basicData/homePageNewsView.jsp";
	private final String  knowledgeBaseView = "/jsp/claim/basicData/knowledgeBaseView.jsp";
	/********
	 * 首页新闻跳转页面
	 */
	public void mainNews(){
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		act.setOutData("LONGON_USER_ID", logonUser.getUserId());
		if(Utility.testString(logonUser.getOemCompanyId())){ //判断是车厂用户还是经销商 YH 2010.12.3
			act.setForword(mainNewsDealer);
		}else {
		    act.setForword(mainNews);
		}
	}
	/****
	 * 经销商端首页新闻
	 */
	public void mainNewsDealer(){
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		act.setOutData("LONGON_USER_ID", logonUser.getUserId());
		act.setForword(mainNewsDealer);
	}
	
	/******
	 * 分页查询
	 * @throws Exception 
	 */
	public void HomePageNewsQuary() {
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try{
		String comman = request.getParamValue("comman");
		String dealerId = logonUser.getDealerId();
		String newsCode = request.getParamValue("newsCode");
		String person = request.getParamValue("person");
		String startDate = request.getParamValue("APPLY_DATE_START");
		String endDate = request.getParamValue("APPLY_DATE_END");
		String newsStatus = request.getParamValue("NEWS_STATUS");
		Integer curPage = request.getParamValue("curPage") != null ? Integer
				.parseInt(request.getParamValue("curPage"))
				: 1; // 处理当前页
				PageResult<Map<String, Object>> ps = dao.homepageNewsList(logonUser,Constant.PAGE_SIZE, curPage, dealerId, newsCode,person,startDate,endDate,newsStatus,comman);
				act.setOutData("LONGON_USER_ID", logonUser.getUserId());
				act.setOutData("ps", ps);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"首页新闻查询失败");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	
	public void HomePageNewsQuary2() { //YH.2010.12.21
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try{
		String comman = request.getParamValue("comman");
		String dealerId = logonUser.getDealerId();
		String newsCode = request.getParamValue("newsCode");
		String person = request.getParamValue("person");
		String startDate = request.getParamValue("APPLY_DATE_START");
		String endDate = request.getParamValue("APPLY_DATE_END");
		String newsStatus = request.getParamValue("NEWS_STATUS");
		String title = request.getParamValue("title");
		Integer curPage = request.getParamValue("curPage") != null ? Integer
				.parseInt(request.getParamValue("curPage"))
				: 1; // 处理当前页
				PageResult<Map<String, Object>> ps = dao.homepageNewsList2(logonUser,Constant.PAGE_SIZE, curPage, dealerId, newsCode,person,startDate,endDate,newsStatus,comman,title);
				act.setOutData("LONGON_USER_ID", logonUser.getUserId());
				act.setOutData("ps", ps);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"首页新闻查询失败");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/**********
	 * 经销商端
	 * 首页新闻查询
	 * 
	 */
	public void HomePageNewsQuaryDealer() {
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try{
		String comman = request.getParamValue("comman");
		String dealerId = logonUser.getDealerId();
		String newsCode = request.getParamValue("newsCode");
		String person = request.getParamValue("person");
		String startDate = request.getParamValue("APPLY_DATE_START");
		String endDate = request.getParamValue("APPLY_DATE_END");
		String newsStatus = request.getParamValue("NEWS_STATUS");
		Integer curPage = request.getParamValue("curPage") != null ? Integer
				.parseInt(request.getParamValue("curPage"))
				: 1; // 处理当前页
				PageResult<Map<String, Object>> ps = dao.homepageNewsList(logonUser,Constant.PAGE_SIZE, curPage, dealerId, newsCode,person,startDate,endDate,newsStatus,comman);
				act.setOutData("LONGON_USER_ID", logonUser.getUserId());
				act.setOutData("ps", ps);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"首页新闻查询失败");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/********
	 * 新增首页新闻界面
	 */
	public void addNews(){
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try{
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String date = format.format(new Date());//获取当前时间
		long userId = logonUser.getUserId();//获取用户ID
		HomePageNewsDao HoDao = new HomePageNewsDao();//获取用户名和公司名称
		List<Map<String,Object>> list = HoDao.getUserName(String.valueOf(userId));
		TtAsWrNewsPO po = new TtAsWrNewsPO();
		String newsCode = SequenceManager.getSequence2("SYXW");
		List<Map<String, Object>> listtype=dao.dutyTypeDQ();//获取所有有效的大区
		act.setOutData("list", list.get(0));
		act.setOutData("listtype", listtype);
		act.setOutData("date", date);
		act.setOutData("newsCode", newsCode);
		act.setOutData("flag", 1);//新增
		act.setForword(addNews);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"首页新闻新增失败");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/*******
	 * 点击修改执行操作
	 */
	public void modifyNews(){
		try{
		String newsId = request.getParamValue("newsId");//获取新闻ID
		List<Map<String, Object>> list = dao.newslist(newsId);//查询首页新闻信息
		List<Map<String, Object>> listtype=dao.dutyTypeDQ();//获取所有有效的大区
		FsFileuploadPO detail = new FsFileuploadPO();
		detail.setYwzj(Long.valueOf(newsId));
		List<FsFileuploadPO> lists = dao.select(detail);
		List<Map<String, Object>> listOrgs = dao.listNewsParentOrgs(newsId);
		String code="";
		if(listOrgs.size()>0){
			for(int i=0;i<listOrgs.size();i++){
				code=listOrgs.get(i).get("DEALER_CODE")+","+code;
			}
		}
		String org_ids="";
		if(list.size()>0){
			if(list.get(0).get("ORG_TYPE_ID")!=null&&list.get(0).get("ORG_TYPE_ID")!=null){
		 org_ids= list.get(0).get("ORG_TYPE_ID").toString();//获取大区ID
				}
		}
		String orgCodes="";
		if(org_ids!=null&&!org_ids.equals("")) {
			String[] orgIds = org_ids.split(",");
			for(int i=0;i<orgIds.length;i++){
				List<Map<String, Object>> listOrgsCode = dao.listNewsParentOrgsCode(orgIds[i]);
					if(listOrgsCode.get(0).get("ORG_CODE")!=null&&listOrgsCode.get(0).get("ORG_CODE")!=null){
						orgCodes = listOrgsCode.get(0).get("ORG_CODE").toString()+","+orgCodes;
					}
			}
		}
		if(code!=null&&!code.equals("")){
			act.setOutData("orgCodes", code.substring(0, code.length()-1));
		}
		if(orgCodes!=null&&!orgCodes.equals("")){
			act.setOutData("codesorg", orgCodes.substring(0, orgCodes.length()-1));
		}
			act.setOutData("list", list.get(0));
			act.setOutData("listtype", listtype);
			act.setOutData("lists",lists);
			act.setForword(modifyNews);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"首页新闻修改失败");
			act.setException(e1);
		}
	}
	/*********
	 * 查询页面
	 */
	public void viewNews(){
		try{
			String newsId = request.getParamValue("newsId");//获取新闻ID
			List<Map<String, Object>> list = dao.newslist(newsId);//查询首页新闻信息
			String comman = request.getParamValue("comman");
			FsFileuploadPO detail = new FsFileuploadPO();
			detail.setYwzj(Long.valueOf(newsId));
			List<FsFileuploadPO> lists = dao.select(detail);
			act.setOutData("comman", comman);
			act.setOutData("list", list.get(0));
			act.setOutData("lists",lists);
			act.setForword(viewNews);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"首页新闻查询失败");
		
		act.setException(e1);
		}
	}
	
	/********
	 * 保存时执行
	 * @throws Exception
	 */
	public void saveNews() throws Exception{
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try{
			String newsCode	 = request.getParamValue("newsCode");//获取新闻编码
			String orgId = request.getParamValue("companyId");//获取机构ID;
			String orgName = request.getParamValue("companyName");//获取机构名称
			String name = request.getParamValue("name");//发表人
			String dept = request.getParamValue("dept");//主管部门
			String title = request.getParamValue("title");//标题
			String contents = request.getParamValue("contents");//内容
			String newDate = request.getParamValue("newDate");//发布时间
			String newsId = request.getParamValue("newsId");//新闻ID 用于修改页面
			String dealerCodeS = request.getParamValue("dealerCode");
			String newsType = request.getParamValue("NEWS_TYPE");//新闻类别
			String checkType = request.getParamValue("viewNewsType");//获取选中的查看类型
			String orgIds = request.getParamValue("orgId");//获取所选大区
			String msg_type = request.getParamValue("Msg_Type"); //获取所选消息类型
			String passMsgtype = null; //如果指定了经销商就获取经销商类型
			
			//新增的时候
			Long newsIdS=0L;
			TtAsWrNewsPO po = new TtAsWrNewsPO();
			po.setContents(contents);
			po.setNewsCode(newsCode);
			po.setVoicePerson(name);
			po.setOrgId(Long.valueOf(orgId));
			po.setOrgName(orgName);
			po.setNewsTitle(title);
			po.setDutyType(Integer.valueOf(checkType));//查看权限
			if(orgIds!=null&&!orgIds.equals("")){
				po.setOrgTypeId(orgIds);//区域ID
			}
			if(msg_type!=null&&!msg_type.equals("")){ // 消息类型 YH 2010.12.24
				if(null != dealerCodeS){
					passMsgtype = this.getDealerTypeByDealerCode(dealerCodeS).toString();
					if(Constant.DEALER_TYPE_DWR == Long.parseLong(passMsgtype)){
						po.setMsgType(Constant.MSG_TYPE_2);
						po.setOrgTypeId(this.getOrgIdByDealerCode(dealerCodeS));
						po.setIsPrivate("1");
					 }else {
						 po.setMsgType(Constant.MSG_TYPE_1);
						 po.setOrgTypeId(this.getOrgIdByDealerCode(dealerCodeS));
						 po.setIsPrivate("1");
					 }								
				}else {
				    po.setMsgType(msg_type);
				    po.setIsPrivate("0");
				}
			}
			if(newsType!=null&&!newsType.equals("")){
				po.setNewsType(Integer.valueOf(newsType));
			}
			
			po.setStatus(Constant.NEWS_STATUS_1);//将状态置为正常
			if(newsId!=null&&!newsId.equals("")){
				newsIdS = Long.valueOf(newsId);
				po.setUpdateDate(new Date());
				po.setUpdateBy(logonUser.getUserId());
			}else{
				 newsIdS = Utility.getLong(SequenceManager.getSequence(""));
				 po.setNewsId(newsIdS);
				 po.setCreateDate(new Date());
				 po.setNewsDate(new Date());//发布时间
				 po.setCreateBy(logonUser.getUserId());
				 if(null != dealerCodeS  ){
					 passMsgtype = this.getDealerTypeByDealerCode(dealerCodeS).toString();
					 if(Constant.DEALER_TYPE_DWR == Long.parseLong(passMsgtype)){
						 po.setMsgType(passMsgtype);
						 po.setOrgTypeId(this.getOrgIdByDealerCode(dealerCodeS));
						 po.setIsPrivate("1");
					 }else {
						 po.setMsgType(Constant.MSG_TYPE_1);
						 po.setOrgTypeId(this.getOrgIdByDealerCode(dealerCodeS));
						 po.setIsPrivate("1");
					 }
					
				 }
				 HomePageNewsDao.insertNewsPO(po);//insert操作	
			}
			/****
			 * 判断权限类型插入权限表
			 */
			//如果修改的话 就先删除权限在查并更新首页新闻
			if(newsId!=null&&!newsId.equals("")){
				dao.delNewsOrg(newsId);//删出权限表
				dao.updateNews(newsId);//更新首页新闻的查看权限
				TtAsWrNewsPO po1 = new TtAsWrNewsPO();
				po1.setNewsId(Long.valueOf(newsId));	
				HomePageNewsDao.updateNewsPO(po1, po);//update操作
			}
			if(checkType.equals(String.valueOf(Constant.VIEW_NEWS_type_1))){//如果是选着经销商端
				if(dealerCodeS!=null){
				String[] dealerCS=dealerCodeS.split(",");
					for(int i=0;i<dealerCS.length;i++){
						//将经销商对应的新闻ID 插入到新闻权限表中··········································
						List<Map<String, Object>> ListDealerId=dao.viewDealer(dealerCS[i]);
						TtAsWrNewsOrgPO orgPo = new TtAsWrNewsOrgPO();
						orgPo.setId(Utility.getLong(SequenceManager.getSequence("")));
						orgPo.setCreateDate(new Date());
						orgPo.setCreateBy(logonUser.getUserId());
						orgPo.setDealerId(Long.valueOf(String.valueOf(ListDealerId.get(0).get("DEALER_ID"))));
						orgPo.setNewsId(newsIdS);
						orgPo.setNewsCode(newsCode);
						 if(null != dealerCodeS ){ // 经销商的消息类型
							 passMsgtype = this.getDealerTypeByDealerCode(dealerCodeS).toString();
							 if(Constant.DEALER_TYPE_DWR == Long.parseLong(passMsgtype)){
								 orgPo.setMsg_type(passMsgtype);
							 }else {
								 orgPo.setMsg_type(Constant.MSG_TYPE_1);
							 }
							
						 } 
						
						HomePageNewsDao.insertNewsOrgPO(orgPo);
					}
				}
			}	
			
			
			//将附件保存
			String ywzj="";
			if(newsId!=null&&!newsId.equals("")){
				ywzj=newsId;
			}
			else{
		    ywzj = String.valueOf(po.getNewsId());
			}
			String[] fjids = request.getParamValues("fjid");//获取文件ID
				FileUploadManager.fileUploadByBusiness(ywzj, fjids, logonUser);	
			if(newsId!=null&&!newsId.equals("")){//修改的时候
				FileUploadManager.delAllFilesUploadByBusiness(ywzj, fjids);
				FileUploadManager.fileUploadByBusiness(ywzj, fjids, logonUser);	
			}else{
				FileUploadManager.fileUploadByBusiness(ywzj, fjids, logonUser);	
			}
			act.setOutData("LONGON_USER_ID", logonUser.getUserId());
			act.setForword(mainNews);
			} catch (Exception e) {
				BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"首页新闻保存失败");
				logger.error(logonUser,e1);
				act.setException(e1);
			}
	}
	
	/********
	 * 关闭操作
	 */
	public void closeNews(){
		String newsId = request.getParamValue("newsId");//获取要关闭的新闻ID
		dao.closeNews(newsId);
		act.setOutData("flag", true);
	}
	
	/********
	 * 作废操作
	 */
	public void  cancelNews(){
		String newsId = request.getParamValue("newsId");//获取要关闭的新闻ID
		dao.cancelNews(newsId);
		act.setOutData("flag", true);
	}
	
	/********
	 * 删除操作  YH 2011.3.23
	 */
	public void  DropNews(){
		String newsId = request.getParamValue("newsId");//获取要关闭的新闻ID
		dao.delNewsOrg(newsId);
		dao.delNews(newsId);
		act.setOutData("flag", true);
	}
	/********
	 * 置顶操作  YH 2011.3.23
	 */
	public void  setTopNews(){
		String newsId = request.getParamValue("newsId");//获取要置顶的新闻ID
		dao.setTop(newsId);
		act.setOutData("flag", true);
	}
   /********
	 * 取消置顶操作  YH 2011.3.23
	 */
	public void dropTopNews() {
		String newsId = request.getParamValue("newsId");//获取要取消置顶的新闻ID
		dao.dropTop(newsId);
		act.setOutData("flag", true);		
	}	
	
	@Override
	protected PO wrapperPO(ResultSet rs, int idx) {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * Iverson 2010-12-08
	 * 应用知识库
	 */
	public void ApplicationKnowledgeBase(){
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		act.setOutData("LONGON_USER_ID", logonUser.getUserId());
		

		//判断登陆系统
		List<Map<String,Object>> ListCode =   cbmDao.queryTcCode();
	    Integer code = Integer.valueOf(ListCode.get(0).get("CODE_ID").toString());
	    if(code==Constant.chana_jc){
			TtAsWrModelGroupPO mg = new TtAsWrModelGroupPO();
			mg.setWrgroupType(Constant.WR_MODEL_GROUP_TYPE_01);
			List<TtAsWrModelGroupPO> listModelGroup = factory.select(mg);
			act.setOutData("listModelGroup", listModelGroup);
	    }
			
		if(Utility.testString(logonUser.getOemCompanyId())){ //判断是车厂用户还是经销商 YH 2010.12.3
			act.setForword(knowledgeBaseDealer);
		}else {

		    act.setForword(ApplicationKnowledgeBase);
		}
	}
	
	/**
	 * Iverson 2010-12-08
	 * 应用知识库
	 * @throws Exception 
	 */
	public void HomePageNewsQuary1() {
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try{
		String comman = request.getParamValue("comman");
		String newsCode = request.getParamValue("newsCode");
		String person = request.getParamValue("person");
		String startDate = request.getParamValue("APPLY_DATE_START");
		String endDate = request.getParamValue("APPLY_DATE_END");
		String newsStatus = request.getParamValue("NEWS_STATUS");
		String title = request.getParamValue("title");
		String modelGroupId = CommonUtils.checkNull(request.getParamValue("modelGroupSel"));
		String modelPart = CommonUtils.checkNull(request.getParamValue("modelPartSel"));
		Integer curPage = request.getParamValue("curPage") != null ? Integer
				.parseInt(request.getParamValue("curPage"))
				: 1; // 处理当前页
				PageResult<Map<String, Object>> ps = dao.homepageNewsList1(Constant.PAGE_SIZE, curPage, newsCode,person,startDate,endDate,newsStatus,comman,title,modelGroupId,modelPart);
				act.setOutData("LONGON_USER_ID", logonUser.getUserId());
				act.setOutData("ps", ps);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"首页新闻查询失败");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/**
	 * Iverson 2010-12-08
	 * 新增应用知识库界面
	 */
	public void addApplicationKnowledgeBase(){
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try{
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String date = format.format(new Date());//获取当前时间
		long userId = logonUser.getUserId();//获取用户ID
		HomePageNewsDao HoDao = new HomePageNewsDao();//获取用户名和公司名称
		List<Map<String,Object>> list = HoDao.getUserName(String.valueOf(userId));
		String newsCode = SequenceManager.getSequence2("SYXW");
		//List<Map<String, Object>> listtype=dao.dutyTypeDQ();//获取所有有效的大区
		act.setOutData("list", list.get(0));
		//act.setOutData("listtype", listtype);
		act.setOutData("date", date);
		act.setOutData("newsCode", newsCode);
		act.setOutData("flag", 1);//新增
		
		//判断登陆系统
		List<Map<String,Object>> ListCode =   cbmDao.queryTcCode();
	    Integer code = Integer.valueOf(ListCode.get(0).get("CODE_ID").toString());
	    if(code==Constant.chana_jc){
			//查询车型组
			TtAsWrModelGroupPO mg = new TtAsWrModelGroupPO();
			mg.setWrgroupType(Constant.WR_MODEL_GROUP_TYPE_01);
			List<TtAsWrModelGroupPO> listModelGroup = factory.select(mg);
			act.setOutData("listModelGroup", listModelGroup);
			//查询车型组
	    }
		
		act.setForword(addApplicationKnowledgeBase);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"首页新闻新增失败");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/**
	 * Iverson 2010-12-09
	 * 保存时执行
	 * @throws Exception
	 */
	public void saveApplicationKnowledgeBase() throws Exception{
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try{
			String newsCode	 = request.getParamValue("newsCode");//获取新闻编码
			String orgId = request.getParamValue("companyId");//获取机构ID;
			String orgName = request.getParamValue("companyName");//获取机构名称
			String name = request.getParamValue("name");//发表人
			String title = request.getParamValue("title");//标题
			String contents = request.getParamValue("contents");//内容
			String newsId = request.getParamValue("newsId");//新闻ID用于(用于判断是新增还是修改，空为新增)
			
			String modelGroupId = CommonUtils.checkNull(request.getParamValue("modelGroupSel"));
			String modelPart = CommonUtils.checkNull(request.getParamValue("modelPartSel"));
			
			//新增的时候
			//Long newsIdS=0L;
			TtAsWrKnowledgebasePO po = new TtAsWrKnowledgebasePO();
			po.setContents(contents);
			po.setCode(newsCode);
			po.setVoicePerson(name);
			po.setOrgId(Long.valueOf(orgId));
			po.setOrgName(orgName);
			po.setTitle(title);
			po.setStatus(Constant.NEWS_STATUS_1);//将状态置为正常
			//if(newsId!=null&&!newsId.equals("")){
				//newsIdS = Long.valueOf(newsId);
				//po.setUpdateDate(new Date());
				//po.setUpdateBy(logonUser.getUserId());
			//}else{
				 //newsIdS = Utility.getLong(SequenceManager.getSequence(""));
				 //po.setId(newsIdS);
			po.setId(Long.parseLong(SequenceManager.getSequence("")));
			po.setCreateDate(new Date());
			po.setPublishedDate(new Date());//发布时间
			po.setCreateBy(logonUser.getUserId());
			if(!"".equals(modelGroupId)){
				po.setWrgroupId(Long.parseLong(modelGroupId));
				po.setModelPart(Long.parseLong(modelPart));
			}
			
			HomePageNewsDao.insert(po);//insert操作	
			//}
			
			//将附件保存
			String ywzj="";
			if(newsId!=null&&!newsId.equals("")){
				ywzj=newsId;
			}
			else{
				ywzj = String.valueOf(po.getId());
			}
			String[] fjids = request.getParamValues("fjid");//获取文件ID
				FileUploadManager.fileUploadByBusiness(ywzj, fjids, logonUser);	
			if(newsId!=null&&!newsId.equals("")){//修改的时候
				FileUploadManager.delAllFilesUploadByBusiness(ywzj, fjids);
				FileUploadManager.fileUploadByBusiness(ywzj, fjids, logonUser);	
			}else{
				FileUploadManager.fileUploadByBusiness(ywzj, fjids, logonUser);	
			}
			act.setOutData("LONGON_USER_ID", logonUser.getUserId());
			act.setForword(ApplicationKnowledgeBase);
			} catch (Exception e) {
				BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"首页新闻保存失败");
				logger.error(logonUser,e1);
				act.setException(e1);
			}
	}
	
	/**
	 * Iverson 2010-12-09
	 * 修改时执行
	 * @throws Exception
	 */
	public void updateApplicationKnowledgeBase() throws Exception{
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try{
			String title = request.getParamValue("title");//标题
			String contents = request.getParamValue("contents");//内容
			String newsId = request.getParamValue("newsId");//新闻ID用于(用于判断是新增还是修改，空为新增)
			String modelPart = CommonUtils.checkNull(request.getParamValue("modelPartSel"));
			String wrgroupId = CommonUtils.checkNull(request.getParamValue("modelGroupSel"));
			//新增的时候
			TtAsWrKnowledgebasePO po = new TtAsWrKnowledgebasePO();
			po.setId(Long.parseLong(newsId));
			TtAsWrKnowledgebasePO poValue = new TtAsWrKnowledgebasePO();
			poValue.setContents(contents);
			poValue.setTitle(title);
			poValue.setUpdateDate(new Date());
			poValue.setUpdateBy(logonUser.getUserId());
			
			if(!"".equals(wrgroupId)){
				poValue.setModelPart(Long.parseLong(modelPart));
				poValue.setWrgroupId(Long.valueOf(wrgroupId));
			}

			
			HomePageNewsDao.update(po, poValue);//update操作	
			
			//将附件保存
			String ywzj=newsId;
			String[] fjids = request.getParamValues("fjid");//获取文件ID
				FileUploadManager.fileUploadByBusiness(ywzj, fjids, logonUser);	
			if(newsId!=null&&!newsId.equals("")){//修改的时候
				FileUploadManager.delAllFilesUploadByBusiness(ywzj, fjids);
				FileUploadManager.fileUploadByBusiness(ywzj, fjids, logonUser);	
			}else{
				FileUploadManager.fileUploadByBusiness(ywzj, fjids, logonUser);	
			}
			act.setOutData("LONGON_USER_ID", logonUser.getUserId());
			act.setForword(ApplicationKnowledgeBase);
			} catch (Exception e) {
				BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"首页新闻保存失败");
				logger.error(logonUser,e1);
				act.setException(e1);
			}
	}
	
	/**
	 * Iverson 2010-12-09
	 * 关闭操作
	 */
	public void closeApplicationKnowledgeBase(){
		String newsId = request.getParamValue("newsId");//获取要关闭的新闻ID
		dao.closeNews1(newsId);
		act.setOutData("flag", true);
	}
	/**
	 * Iverson 2010-12-09
	 * 查询页面
	 */
	public void viewNews1(){
		try{
			String newsId = request.getParamValue("newsId");//获取新闻ID
			List<Map<String, Object>> list = dao.newslist1(newsId);
			String comman = request.getParamValue("comman");
			FsFileuploadPO detail = new FsFileuploadPO();
			detail.setYwzj(Long.valueOf(newsId));
			List<FsFileuploadPO> lists = dao.select(detail);
			
			//判断登陆系统
			List<Map<String,Object>> ListCode =   cbmDao.queryTcCode();
		    Integer code = Integer.valueOf(ListCode.get(0).get("CODE_ID").toString());
		    if(code==Constant.chana_jc){
				//查询车型组
				if(list.get(0).get("WRGROUP_ID")!=null){
					TtAsWrModelGroupPO mg = new TtAsWrModelGroupPO();
					mg.setWrgroupId(Long.parseLong(list.get(0).get("WRGROUP_ID").toString()));
					List<TtAsWrModelGroupPO> listModelGroup = factory.select(mg);
					act.setOutData("listModelName", listModelGroup.get(0).getWrgroupName());
				}
				//查询车型组
		    }
			
			act.setOutData("comman", comman);
			act.setOutData("list", list.get(0));
			act.setOutData("lists",lists);
			
			act.setForword(knowledgeBaseView);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"首页新闻查询失败");
			act.setException(e1);
		}
	}
	/**
	 * Iverson 2010-12-09
	 * 点击修改执行操作
	 */
	public void modifyNews1(){
		try{
		String newsId = request.getParamValue("newsId");//获取新闻ID
		List<Map<String, Object>> list = dao.newslist1(newsId);
		FsFileuploadPO detail = new FsFileuploadPO();
		detail.setYwzj(Long.valueOf(newsId));
		List<FsFileuploadPO> lists = dao.select(detail);
		
		//判断登陆系统
		List<Map<String,Object>> ListCode =   cbmDao.queryTcCode();
	    Integer code = Integer.valueOf(ListCode.get(0).get("CODE_ID").toString());
	    if(code==Constant.chana_jc){
			//查询车型组
			
			TtAsWrModelGroupPO mg = new TtAsWrModelGroupPO();
			mg.setWrgroupType(Constant.WR_MODEL_GROUP_TYPE_01);
			List<TtAsWrModelGroupPO> listModelGroup = factory.select(mg);
			act.setOutData("listModelGroup", listModelGroup);
	    
			if(list.get(0).get("WRGROUP_ID")!=null){
				TtAsWrModelGroupPO mg2 = new TtAsWrModelGroupPO();
				mg2.setWrgroupId(Long.parseLong(list.get(0).get("WRGROUP_ID").toString()));
				List<TtAsWrModelGroupPO> listModelGroupPo = factory.select(mg2);
				act.setOutData("listModelId", listModelGroupPo.get(0).getWrgroupId());
			}
			//查询车型组
	    }
		act.setOutData("list", list.get(0));
		act.setOutData("lists",lists);
		act.setForword(knowledgeBaseModify);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"首页新闻修改失败");
			act.setException(e1);
		}
	}

	@SuppressWarnings("unchecked")
	public String getOrgIdByDealerCode(String dealerCodes){
		
		String orgids = null;
		String[] dealerCS=dealerCodes.split(",");
		Set setorgids = new HashSet();
		if(dealerCS.length == 1){
			StringBuffer sql= new StringBuffer();
			sql.append("select org_id\n" );
			sql.append("  from Tm_Dealer_Org_Relation do, tm_dealer d\n" );
			sql.append(" where d.dealer_code = '"+dealerCS[0]+"'\n" );
			sql.append("   and do.dealer_id = d.dealer_id");       		
			Map map = dao.pageQueryMap(sql.toString(),null,dao.getFunName());
			if(null != map && map.size()>0){
			  orgids = map.get("ORG_ID").toString();
			}
			return map.get("ORG_ID").toString();
		}else if(dealerCS.length>1){
			StringBuffer orgIds = new StringBuffer();
			
			for(int i=0;i<dealerCS.length;i++){				
				StringBuffer sql= new StringBuffer();
				sql.append("select org_id\n" );
				sql.append("  from Tm_Dealer_Org_Relation do, tm_dealer d\n" );
				sql.append(" where d.dealer_code = '"+dealerCS[i]+"'\n" );
				sql.append("   and do.dealer_id = d.dealer_id");       		
				Map map = dao.pageQueryMap(sql.toString(),null,dao.getFunName());
				if(null !=map && map.size()>0){
				   setorgids.add(map.get("ORG_ID").toString());
				}
			}			
			Iterator it = setorgids.iterator();   
			while(it.hasNext()){   			   
				orgIds.append((String)it.next()+",");
			   }
			
			orgids = orgIds.substring(0, orgIds.length()-1);
		}		
		 return orgids;
	}
	
	@SuppressWarnings("unchecked")
	public Integer getDealerTypeByDealerCode(String dealerCodes){
		TmDealerPO dp = new TmDealerPO();
		String[] dealerCS=dealerCodes.split(",");
		dp.setDealerCode(dealerCS[0]);
		List list = dao.select(dp);
		dp = (TmDealerPO)list.get(0);		
		return dp.getDealerType();
	}
}
